import cn.maple.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ESCtest {

        @Test
        public void testAdd() throws Exception{
            // 1.获取ES连接
            TransportClient client = ESClientUtil.getClient();
            // 2.准备要放入到ES中的数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", "2");
            map.put("name", "哈哈哈");
            map.put("age", "3");
            map.put("sex", "1");
            // 3.使用链式编程，将数据存入到ES中
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", "2").setSource(map);
            // 4.得到添加文档结果
            IndexResponse xx =builder.setSource(map).get();
            // 5.关闭连接
            System.out.println(xx);
            client.close();

    }


    //查询
    @Test
    public void Get() throws Exception {
        // 1.获取到ES核心对象
        TransportClient client = ESClientUtil.getClient();
        GetRequestBuilder builder = client.prepareGet("pethome", "user", "1");
        Map<String, Object> source = builder.get().getSource();
        System.out.println(source);
        client.close();
    }


    //修改
    @Test
    public void testUpdate() throws Exception {
        // 1.获取到ES核心对象
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder update = client.prepareUpdate("pethome", "user", "1");
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "还会举办多次撒娇");
        UpdateResponse response = update.setDoc(map).get();
        System.out.println(response);
    }

    @Test
    public void remove() throws Exception {
        // 1.获取到ES核心对象
        TransportClient client = ESClientUtil.getClient();
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "user", "1");
        DeleteResponse response = builder.get();
        System.out.println(response);
    }


    //查询

    @Test
    public void testComplex() throws Exception {
        // 1.获取到ES核心对象
        TransportClient client = ESClientUtil.getClient();
        // 你可以查询多个index
        SearchRequestBuilder builder = client.prepareSearch("pethome");

        // 一个方法要求我们传递一个接口，你有几种方式：1.最瓦的：匿名内部类 2.接口自己的子类 3.以后找接口的对应工具类
        // 得到组合查询对象
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        // 模糊查询
        query.must(QueryBuilders.matchQuery("name", "金"));

        // 等值查询
        query.filter(QueryBuilders.rangeQuery("age").gte(1).lte(12))
                .filter(QueryBuilders.termQuery("sex", 0));

        builder.setQuery(query);
        // 添加分页条件
      //  builder.setFrom(4);
       // builder.setSize(2);
        // 添加排序
        SearchResponse response = builder.addSort("age", SortOrder.DESC).get();

        // 得到查询结果
        SearchHit[] hits = response.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

    }


    @Test
    public void Batchadd() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 20; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "金毛" + i + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }


}
